<?
include_once 'CoreModel.php';
Class UpdateSaleOrderModel extends CoreModel {
	private $table;
	public function __construct() {
		$this -> table = "zsd_web_order";
		parent::__construct();
	}

	public function updateConnectData($result) {
		/* $result is result from sap */
		//	print_r($result);
		//exit();
		// default log message
		$data_log['log_function'] = "ZSDRFC_SALESORDER_DATA";
		$data_log['log_table'] = "ZSD_WEB_ORDER";
		$data_log['log_record'] = 0;
		$data_log['log_type'] = "UPDATE";
		$data_log['log_status'] = "COMPLETED";
		$data_log['log_by_name'] = "Batch Job";
		$data_log['log_page'] = "adminpanel/updatesaleorder";

		$numRows = 0;
		// start begin trans
		$this -> db -> trans_begin();
		//$this -> db -> query('delete from CUSTOMER_DATA');
		$this->db->query("SET NAMES tis620"); 
		$dataUpdate = array();
		$i = 0;
		foreach ($result["WEB_DATA"] as $so) {
			$i++;
			$data = array();
			
			$data['ORDER_NO'] = $so["ORDER_NO"];
			$data['ITEM_NO'] = $so["ITEM_NO"];
			$data['SALES_ORDER_FLAG'] = $so["SALES_ORDER_FLAG"];
			$data['DELIVERY_FLAG'] = $so["DELIVERY_FLAG"];
			$data['BILLING_FLAG'] = $so["BILLING_FLAG"];
			$data['SALES_ORDER_NO'] = $so["SALES_ORDER_NO"];
			$data['DELIVERY_NO'] = $so["DELIVERY_NO"];
			$data['BILLING_NO'] = $so["BILLING_NO"];
			$data['ERROR_LOG'] = $so["ERROR_LOG"];
			$data['CLEARING_DOC'] = $so["CLEARING_DOC"];
			$data['CLEARING_DATE'] = $so["CLEARING_DATE"];
			//$data['SAP_DOCNO'] = $so["SAP_DOCNO"];

			$this->db->where("ORDER_NO",$so['ORDER_NO']);
			$this->db->where("ITEM_NO",$so['ITEM_NO']);
			$this->db->update($this->table,$data);	
		}
		if ($this -> db -> trans_status() === FALSE) {
			// if error on tranaction
			$this -> db -> trans_rollback();
				
			// insert log  table & page
			$this->insertLogFailed('ZSDRFC_SALESORDER_DATA','ZSD_WEB_ORDER', 'adminpanel/updatesaledorder');
			
			return array("flag" => false, "message" => "error on transfer");
		} else {
			// when complete
			$this -> db -> trans_commit();
			$this->insertLogComplete('ZSDRFC_SALESORDER_DATA','ZSD_WEB_ORDER', $i, 'adminpanel/updatesaledorder');
			//$this->insertLogComplete('SALES_DATA', count($result["SALES_DATA"]));		
			//return array("flag" => true, "customer_row" => count($dataInsert), "sale_row" => count($result["SALES_DATA"]));	
			//return ค่าไป controller
			return array("flag" => true, "connect_row" => $i);
		}
	}
	
	function getAll(){
    	$q = $this->db->get('zsd_web_order');

		if($q->num_rows() > 0){
				return $q->result();
		}
	}

}
?>